package xyz.yuanwl.demo.spring.cloud.product.controller;

import xyz.yuanwl.demo.spring.cloud.product.vo.ProductInfoVO;
import xyz.yuanwl.demo.spring.cloud.product.vo.ProductVO;
import xyz.yuanwl.demo.spring.cloud.product.vo.ResultVO;
import xyz.yuanwl.demo.spring.cloud.product.common.DecreaseStockInput;
import xyz.yuanwl.demo.spring.cloud.product.common.ProductInfoOutput;
import xyz.yuanwl.demo.spring.cloud.product.entity.ProductCategory;
import xyz.yuanwl.demo.spring.cloud.product.entity.ProductInfo;
import xyz.yuanwl.demo.spring.cloud.product.service.CategoryService;
import xyz.yuanwl.demo.spring.cloud.product.service.ProductService;
import xyz.yuanwl.demo.spring.cloud.product.utils.ResultVOUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品
 * Created by 廖师兄
 * 2017-12-09 21:13
 */
@RestController
@RequestMapping
public class ProductController {

	@Autowired
	private ProductService productService;

	@Autowired
	private CategoryService categoryService;

	/**
	 * 1. 查询所有在架的商品
	 * 2. 获取类目type列表
	 * 3. 查询类目
	 * 4. 构造数据
	 */
	@GetMapping("list")
	public ResultVO<ProductVO> list() {
		//触发降级准备的代码
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}


		//1. 查询所有在架的商品
		List<ProductInfo> productInfoList = productService.findUpAll();

		//2. 获取类目type列表
		List<Integer> categoryTypeList = productInfoList.stream()
				.map(ProductInfo::getCategoryType)
				.collect(Collectors.toList());

		//3. 从数据库查询类目
		List<ProductCategory> categoryList = categoryService.findByCategoryTypeIn(categoryTypeList);

		//4. 构造数据
		List<ProductVO> productVOList = new ArrayList<>();
		for (ProductCategory productCategory : categoryList) {
			ProductVO productVO = new ProductVO();
			productVO.setCategoryName(productCategory.getCategoryName());
			productVO.setCategoryType(productCategory.getCategoryType());

			List<ProductInfoVO> productInfoVOList = new ArrayList<>();
			for (ProductInfo productInfo : productInfoList) {
				if (productInfo.getCategoryType().equals(productCategory.getCategoryType())) {
					ProductInfoVO productInfoVO = new ProductInfoVO();
					BeanUtils.copyProperties(productInfo, productInfoVO);
					productInfoVOList.add(productInfoVO);
				}
			}
			productVO.setProductInfoVOList(productInfoVOList);
			productVOList.add(productVO);
		}

		return ResultVOUtil.success(productVOList);
	}

	@GetMapping("/msg")
	public String msg(HttpServletRequest request) {
		System.err.println(request.getCookies());
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return "this is product0's msg";
	}

	/**
	 * 获取商品列表(给订单服务用的)
	 *
	 * @param productIdList
	 * @return
	 */
	@PostMapping("listForOrder")
	public List<ProductInfoOutput> listForOrder(@RequestBody List<String> productIdList) {
		return productService.findList(productIdList);
	}

	@PostMapping("decreaseStock")
	public void decreaseStock(@RequestBody List<DecreaseStockInput> decreaseStockInputList) {
		productService.decreaseStock(decreaseStockInputList);
	}
}
